<?php

    //nzo2 message input/output system
	class IO_Exception extends Exception {
	}
	
	interface i_io_layer {
		public function get_input($name, $type);
		public function output($msg);
	}
	
	define ("IO_DEBUG_NONE", 	0);
	define ("IO_DEBUG_ERROR", 	1);
	define ("IO_DEBUG_WARNING", 2);
	define ("IO_DEBUG_NOTICE", 	3);
	
	
	
	define ("IO_DEBUG_FULL", 	100);

	 
	class io extends service implements i_io_layer {
		
		private $__poi;
		
		const IO_INPUT_DEFAULT_TYPE = "str";

		
		public function __construct(){
			switch (php_sapi_name()){
				
				case "cli":
					$this->__poi = new cli_io();
				break;
				
				default:
					$this->__poi = new cgi_io();
				break;
			}
		}
		
		public function get($name, $type = null){
			return $this->get_input($name, $type);
		}
		
		public function get_input($name, $type){
			if (!$type){
				$type = self::IO_INPUT_DEFAULT_TYPE;
			}
			return $this->__poi->get_input($name, $type);
		}
		
		
		//--------------------------------------------------------------
		public function get_debug_level(){
			#return IO_DEBUG_NOTICE;
			$level = context::get("config")->get["env"]["debug_level"];
			if (isset($level)){
				return (int)$level;
			}
			return IO_DEBUG_FULL;
		}
		
		private function UTIL_get_trace(){
			$trace  = debug_backtrace();
			$caller = isset( $trace[1] ) ? $trace[1] : array();
			$owner  = isset( $trace[2] ) ? $trace[2] : array();

			$file  = isset( $caller['file'] ) ? $caller['file'] : null;
			
			#$class 		= isset( $owner['class'] )? $owner['class']: null;
			#$function 	= isset( $owner['function'] ) ? $owner['function'] : null;
			$line 		= isset( $caller['line'] )? $caller['line'] : null;
			
			return array("file"=>$file,"line"=>$line);
		}
		
		public function trace(){
			#$trace  = debug_backtrace();
			if (!$this->is_cli()){
				echo "<pre>";
				debug_print_backtrace();
				echo "</pre>";
			}else{
				debug_print_backtrace();
			}
		}
		
		public function dump($var){
			$this->msg("dumping variable:");
	
			$dump = print_r($var,true);
		
			if (!$this->is_cli()){
				
				$msg = "<pre>{$dump}</pre>";
			}else{
				$msg = $dump;
			}
			
			$this->msg($msg);
		}
		
		public function debug($msg, $level = IO_DEBUG_NOTICE){
			
			
			if ($this->get_debug_level() >= $level){
				
				$trace = $this->UTIL_get_trace();
				
				$message = basename($trace["file"]).", line {$trace["line"]} - {$msg}";
				
				$this->msg($message);
			}
		}
		//--------------------------------------------------------------
		
		public function msg($msg){	//allias for output()
			$this->output($msg);
		}
		
		public function output($msg){
			$this->__poi->output($msg);
		}
		
		private function is_cli(){
			return (php_sapi_name() == 'cli' );
		}
		
		
	}
	
	//apache io abstraction
	class cli_io implements i_io_layer {
		public function get_input($name, $type){

		}
		
		public function output($msg){
			echo $msg."\n";
		}
	}
	
	//console io abstraction
	class cgi_io implements i_io_layer {
		public function get_input($name, $type){
			if (isset($_REQUEST[$name])){
				$value = $_REQUEST[$name];
				
				switch ($type) {
					case "int":
						return (int)$value;
					break;
					
					default:
						return $value;
					break;
				}

			}else{
				
			}
		}
		
		public function output($msg){
			echo $msg."<br>";
		}
	}

?>
